//
//  TRCommentLogTableViewCell.m
//  WeiMiLan
//
//  Created by anan on 14/12/14.
//  Copyright (c) 2014年 Mac. All rights reserved.
//

#import "TRCommentLogTableViewCell.h"
#import "DLAVAlertView.h"
@implementation TRCommentLogTableViewCell

- (void)awakeFromNib {
    // Initialization code
}
- (IBAction)praiseBtn:(UIButton *)sender {
    
//    "THUMBS_UP_FLAG" = yes;
    
    NSString *tuhmbs_up_flag = [NSString stringWithFormat:@"%@",self.info[@"THUMBS_UP_FLAG"]];
    NSString *thumbs_up_type = nil;
    
    if ([tuhmbs_up_flag isEqualToString:@"yes"]) {
        
        thumbs_up_type = @"10009";
        
    }else {
        thumbs_up_type = @"10008";
    }
    
    
    NSString *userId = [NSStandardUserDefaults objectForKey:@"USER_ID"];
    NSDictionary *parameters = @{@"userId" : userId,
                                 @"type" : thumbs_up_type,
                                 @"commentId" : [NSString stringWithFormat:@"%zd",self.replyLogBtn.tag]
                                 };
    
    AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
    requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
    [SVProgressHUD show];
    //int productId = [self.info ql_intForKey:@"ID"];
    [requestManager POST:@"http://ping.vsmu.cn/app/Interface/Comment" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dic = responseObject;
        if ([dic ql_intForKey:@"code"]==0000) {
            if ([thumbs_up_type isEqualToString:@"10008"]) {
                [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"点赞成功"];
            } else if ([thumbs_up_type isEqualToString:@"10009"]) {
                [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
                [SVProgressHUD showSuccessWithStatus:@"取消点赞成功"];
            }
           
        }
        [[NSNotificationCenter defaultCenter]postNotificationName:@"kSendMessage" object:nil userInfo:nil];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        if ([thumbs_up_type isEqualToString:@"10008"]) {
            [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
            [SVProgressHUD showErrorWithStatus:@"点赞失败"];
        } else if([thumbs_up_type isEqualToString:@"10009"]){
            [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
            [SVProgressHUD showErrorWithStatus:@"取消点赞失败"];
        }
     
    }];


}

- (IBAction)commentLogBtn:(UIButton *)sender {
    
    DLAVAlertView *alertView = [[DLAVAlertView alloc] initWithTitle:@"添加回复" message:nil delegate:nil cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
    UITextView *textView = [[UITextView alloc]init];
    //  textView.backgroundColor = [UIColor redColor];
    textView.frame = CGRectMake(0, 0, 250, 64);
    alertView.contentView = textView;
    [alertView showWithCompletion:^(DLAVAlertView *alertView, NSInteger buttonIndex) {
        NSLog(@"Tapped button '%@' at index: %ld", [alertView buttonTitleAtIndex:buttonIndex], (long)buttonIndex);
        if (buttonIndex == 1) {
            
            NSString *userId = [NSStandardUserDefaults objectForKey:@"USER_ID"];
            NSDictionary *parameters = @{@"userId" : userId,
                                         @"type" : @"10003",
                                         @"commentId" : [NSString stringWithFormat:@"%zd",self.replyLogBtn.tag],
                                         @"productId":self.info[@"PRODUCT_ID"],
                                         @"content" : textView.text
                                         };
            NSLog(@"-------------%@",parameters);
            AFHTTPRequestOperationManager *requestManager = [AFHTTPRequestOperationManager manager];
            requestManager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/html"];
            [SVProgressHUD show];
            //int productId = [self.info ql_intForKey:@"ID"];
            [requestManager POST:@"http://ping.vsmu.cn/app/Interface/Comment" parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
                NSDictionary *dic = responseObject;
                if ([dic ql_intForKey:@"code"]==0000) {
                    [SVProgressHUD showSuccessWithStatus:@"评论发表成功"];
                }
                [[NSNotificationCenter defaultCenter]postNotificationName:@"kSendMessage" object:nil userInfo:@{@"commentLogBtn": self.replyLogBtn,@"productID": self.productID}];
            } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                [SVProgressHUD showErrorWithStatus:@"评论发表失败"];
            }];
        }
        
    }];

    
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setInfo:(NSDictionary *)info
{
    
    _info = info;
    
    if (!self.isPingLunCell) {
        self.prasieView.hidden = YES;
    } else  {
        self.prasieView.hidden = NO;
    }
    
    self.imageViewUserHeader.layer.masksToBounds = YES;
    self.imageViewUserHeader.layer.cornerRadius = 16; //设置图片圆角的尺度
    [self.imageViewUserHeader setImageWithURL:[NSURL URLWithString:info[@"AUTHOR_HEAD_PIC"]] placeholderImage:[UIImage imageNamed:@"nodata_public"]];
    self.labelUserName.text = info[@"AUTHOR_NAME"];
    NSString *userName = info[@"ANSWER_USERNAME"];
    self.productID = info[@"PRODUCT_ID"];
    self.replyLogBtn.tag = [info[@"ID"] integerValue];
    
     NSString *contnetText = info[@"CONTENT"];
    
    if ([[info allKeys] containsObject:@"THUMBS_UP"]) {
        
        NSInteger praiseCount = [info[@"THUMBS_UP"] integerValue];
        if (praiseCount == 0) {
           
            [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-zan-(5)"] forState:UIControlStateNormal];
        } else if (praiseCount > 0) {
             self.praiseCount.text = [NSString stringWithFormat:@"%zd",praiseCount];
             [self.praiseImageBtn setBackgroundImage:[UIImage imageNamed:@"赞"] forState:UIControlStateNormal];
        }
    }
    
    
    
    if (userName) {
        NSString *header = [NSString stringWithFormat:@"%@ 回复了 ",userName];
        NSString *content = [NSString stringWithFormat:@"%@%@",header,contnetText];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.659 green:0.541 blue:0.306 alpha:1.000] range:NSMakeRange(0,userName.length)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.4 alpha:1.000] range:NSMakeRange(userName.length,3)];
        self.labelContent.attributedText = str;
    }else{
        userName = info[@"AUTHOR_NAME"];
        NSString *header = [NSString stringWithFormat:@"%@ 评论了 ",userName];
        NSString *content = [NSString stringWithFormat:@"%@%@",header,contnetText];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:content];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithRed:0.659 green:0.541 blue:0.306 alpha:1.000] range:NSMakeRange(0,userName.length)];
        [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithWhite:0.4 alpha:1.000] range:NSMakeRange(userName.length,3)];
        self.labelContent.attributedText = str;
    }
    self.replyCounts.text = [NSString stringWithFormat:@"%@",info[@"REPLIES"]];
    
   
  //  CGRect r = self.labelContent.frame;
 //   r.size.width = 264;
   // self.labelContent.frame = r;
    [self.labelContent sizeToFit];
    
    CGRect frame = self.frame;
    frame.size.height = CGRectGetMaxY(self.labelContent.frame) + 58;//self.labelContent.frame.origin.y+self.labelContent.frame.size.height+10;
    self.frame = frame;
    
//    CGRect lr = self.imageViewLine.frame;
//    lr.origin.y = frame.size.height-1;
//    self.imageViewLine.frame = lr;
    
    self.labelTime.text = self.info[@"CREATE_DATE"];
}

- (float)getHeight
{
    return self.frame.size.height;
}

- (IBAction)buttonUserDetailAction:(id)sender
{
    [QLPlayerManager openUserHomeWithId:[self.info ql_intForKey:@"AUTHOR_ID"] viewController:self.viewController];
}

@end
